package com.wish.blessingroad.controller;


import com.wish.blessingroad.common.ResponseResult;
import com.wish.blessingroad.config.WeChatConfig;
import com.wish.blessingroad.service.impl.WeChatServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.Map;


/**
 * <p>
 * 微信登录控制器
 * </p>
 *
 * @author dhc
 * @since 2019-02-28
 */
@Controller
@RequestMapping("/api/weixin")
public class WeChatController {
    private static final Logger LOG = LoggerFactory.getLogger(WeChatController.class);

    @Autowired
    private WeChatServiceImpl weChatServiceImpl;

    /**
     * 小程序登录code换openid
     */
    @RequestMapping(value = "/wxlogin")
    public Object wxlogin(@NotNull @RequestParam String code) {
        return new ResponseResult<>(weChatServiceImpl.wxlogin(code)).success();
    }

    /**
     * 小程序邀请登录code换openid返回userId
     * @param code
     * @param userIdTo  邀请人id
     * @return
     */
    @RequestMapping(value = "/wxloginInvite")
    public Object wxloginInvite(@NotNull @RequestParam String code,
            @NotNull @RequestParam String userIdTo) {
        return new ResponseResult<>(weChatServiceImpl.wxloginInvite(code, userIdTo)).success();
    }

    /**
     * 微信支付
     * @param userId 用户id
     * @param userName 用户名
     * @param avatarUrl 用户头像
     * @param blessingId 祝福表
     * @param money 钱 单位：分
     * @param request
     * @return
     */
    @RequestMapping(value = "/payment", method = RequestMethod.POST)
    public Map payment(String userId,String userName,String avatarUrl, String blessingId, String money,
            HttpServletRequest request) {
        return weChatServiceImpl.payment(userId, userName, avatarUrl,blessingId, money, request);
    }

    /**
     * 微信小程序支付成功回调函数
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/callback")
    public void wxNotify(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        weChatServiceImpl.wxNotify(request, response);
    }
}

